package com.xszx.serviceedu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xszx.servicebase.exceptionhandler.QinXueException;
import com.xszx.serviceedu.entity.ExcelSubjectData;
import com.xszx.serviceedu.entity.Subject;
import com.xszx.serviceedu.entity.vo.SubjectNestedVo;
import com.xszx.serviceedu.entity.vo.SubjectVo;
import com.xszx.serviceedu.listener.SubjectExcelListener;
import com.xszx.serviceedu.mapper.SubjectMapper;
import com.xszx.serviceedu.service.SubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author lhp
 * @since 2024-07-14
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    //添加课程分类
    @Override
    public void batchImport(MultipartFile file,SubjectService subjectService) {
        try {
            //1 获取文件输入流
            InputStream inputStream = file.getInputStream();

            // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
            EasyExcel.read(inputStream, ExcelSubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch(Exception e) {
            e.printStackTrace();
            throw new QinXueException(20001,"添加课程分类失败");
        }
    }

    @Override
    public List<SubjectNestedVo> nestedList() {//嵌套系统
        //最终要的到的数据列表
        List<SubjectNestedVo> subjectNestedVoList = new ArrayList<>();

        //查询一级分类数据记录
        QueryWrapper<Subject> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("parent_id",0);//父id是0的就说明自己就是一级课程
        List<Subject> list1 = baseMapper.selectList(wrapper1);//查询一级课程

        //查询二级分类数据记录
        QueryWrapper<Subject> wrapper2 = new QueryWrapper<>();
        wrapper2.ne("parent_id",0);//查询父id不是0的说明是二级课程
        List<Subject> list2 = baseMapper.selectList(wrapper2);

        //组装一级分类vo数据
        for (Subject subject : list1) {
            //创建一级类别vo对象
            SubjectNestedVo subjectNestedVo = new SubjectNestedVo();
//            subjectNestedVo.setId(subject.getId());
//            subjectNestedVo.setTitle(subject.getTitle());
            //可优化 就是将subject里面和subjectNestedVo里面对应的属性赋给subjectNestedVo
            BeanUtils.copyProperties(subject, subjectNestedVo);
            subjectNestedVoList.add(subjectNestedVo);

            //创建一级分类集合,便于后续将二级分类传进去
            List<SubjectVo> children =  new ArrayList<>();
            //填充二级分类vo数据
            for (Subject subject1 : list2) {
                //如果二级分类1父id和一级分类的id相等
                if(subject1.getParentId().equals(subject.getId())){
                    //创建二级类别vo对象
                    SubjectVo subjectVo = new SubjectVo();
                    BeanUtils.copyProperties(subject1,subjectVo);
                    children.add(subjectVo);
                }
            }
            subjectNestedVo.setChildren(children);
        }
        return subjectNestedVoList;
    }
}
